/*
 * This file is part of aCSV.
 *
 * aCSV is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aCSV is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with aCSV.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.apelle.acsv;

import org.apache.commons.cli.*;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

/**
 *
 * @author
 * @version $Id$
 */
public class CSVMain {
    private static final String CLASSNAME = CSVMain.class.getName();

    public static void main(String[] args) {
        Options options = new Options();

        Option classname;
        classname = OptionBuilder.withArgName("className")
                .hasArg()
                .isRequired()
                .withDescription("Custom CSV Class Processor")
                .withLongOpt("classname")
                .create("c");

        Option filename;
        filename = OptionBuilder.withArgName("inputFile")
                .hasArg()
                .isRequired()
                .withDescription("File to process with custom className")
                .withLongOpt("input-file")
                .create("i");

        options.addOption(classname);
        options.addOption(filename);

        CommandLineParser cp = new PosixParser();


        CommandLine parse = null;
        try {
            parse = cp.parse(options, args);
        } catch (ParseException e) {
            System.out.println("Unexpected exception:" + e.getMessage());
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("java " + CLASSNAME, options);
            System.exit(-1);
        }

        if (parse == null) {
            System.err.println("PARSE NULL BAH!");
            System.exit(-1);
        }


        String clazz;
        String file;

        clazz = parse.getOptionValue('c');
        file = parse.getOptionValue('i');
        System.out.println("file = " + file);
        System.out.println("classname = " + clazz);

        try {
            CSVProcessor main = new CSVProcessor(clazz);
            main.process(file);
            System.exit(0);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        System.exit(-1);
    }
}
